package com.cfinnovate.portlet;

//portlet APIs
import java.io.IOException;
import java.util.Map;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.PortletRequest;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

public class CfmPortlet extends GenericPortlet {

	private static final long serialVersionUID = 1L;
	public static String CFC_PATH_PARAM = "cfcName";

	@Override
	public void init() throws PortletException {
		System.out.println(getPortletConfig().getInitParameterNames());

		CfmlEngineInteraction cei = getCfmlEngineInteraction();
		installPortletHelperFile();
		String appRoot = getPortletConfig().getInitParameter("appRoot");
		if(appRoot != null)
		{
			cei.installFile("/cfmlRunner.cfm", appRoot + "cfmlRunner.cfm");
		}
	}

	private void installPortletHelperFile() throws PortletException {
		CfmlEngineInteraction cei = getCfmlEngineInteraction();
		cei.installFile("/support/CopyVariables.cfm");
	}

	public void doView(RenderRequest request, RenderResponse response)
			throws PortletException, IOException {
		String view = cleanPath(getInitParameter("view")) + getParametersAsQueryString(request);
		CfmlEngineInteraction cei =  getCfmlEngineInteraction();
		cei.prepVariables(request, response);
		cei.callCfml(request, response, view);
	}

	public void processAction(ActionRequest request, ActionResponse response)
			throws PortletException {
		String path = cleanPath(getInitParameter("action"))
		+ getParametersAsQueryString(request);
		CfmlEngineInteraction cei =  getCfmlEngineInteraction();
		cei.prepVariables(request, response);
		cei.callCfml(request, response, path);
	}

	private String cleanPath(String path) {
		if ("/".equals(path.substring(0, 1))) {
			return path;
		} else {
			return "/" + path;
		}
	}

	@Override
	public String getTitle(RenderRequest request) {
		String title = getPortletConfig().getInitParameter("title");
		if (title == null) {
			title = "Bing!";
		}
		return title;

	}

	private String getParametersAsQueryString(PortletRequest request) {
		String qString = "?";
		Map<String, String[]> parameters = request.getParameterMap();
		for (String key : parameters.keySet()) {
			String[] values = parameters.get(key);
			String value = "";
			for (int i = 0; i < values.length; i++) {
				value += values[i] + ",";
			}
			value = value.substring(0, value.length() - 1);
			qString += key + "=" + value + "&";
		}
		return qString;
	}

	private CfmlEngineInteraction getCfmlEngineInteraction() {
		Object cei = getPortletContext().getAttribute("CfmlEngineInteraction");
		if (!(cei instanceof CfmlEngineInteraction)) {
			cei = new CfmlEngineInteraction(getPortletContext(),
					getPortletConfig());
			getPortletContext().setAttribute("CfmlEngineInteraction", cei);
		}
		return (CfmlEngineInteraction) cei;
	}
}
